import 'package:bruno/bruno.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:sign_tool/view/sign_type_item.dart';


import '../data/apk_info.dart';
import '../data/sign_type.dart';

class SignFormWidget extends StatefulWidget {
  const SignFormWidget({super.key});

  @override
  State<SignFormWidget> createState() => _SignFormWidgetState();
}

class _SignFormWidgetState extends State<SignFormWidget> {
  @override
  Widget build(BuildContext context) {
    var mList = Get.put<SignInfoModel>(SignInfoModel()).getSignInfoList();
    var apkInfoProvider = Provider.of<ApkInfoModel>(context, listen: true);
    return Container(
        margin: const EdgeInsets.all(10),
        padding: const EdgeInsets.all(10),
        width: double.infinity,
        height: 400,
        decoration: ShapeDecoration(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5.0),
                side: const BorderSide(width: 1, color: Colors.blue))),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text(
                "Apk路径:",
                style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                    color: Colors.deepPurple),
              ),
              Container(
                  decoration: const UnderlineTabIndicator(
                      borderSide: BorderSide(width: 2, color: Colors.black12)),
                  child: Text(apkInfoProvider.apkPath ?? "请选择apk")),
              Visibility(
                  visible: apkInfoProvider.apkPath != null,
                  child: IconButton(
                      onPressed: () {
                        apkInfoProvider.apkPath = null;
                      },
                      icon: const Icon(Icons.delete))),
              BrnButtonPanel(
                mainButtonName: "选择apk",
                mainButtonOnTap: () async {
                  FilePickerResult? result =
                      await FilePicker.platform.pickFiles();

                  if (kIsWeb) {
                    // if (result != null) {
                    //   // Uint8List? fileBytes = result.files.first.bytes;
                    //   // String fileName = fileBytes.toString();
                    //   // apkInfoProvider.apkPath = fileName;
                    // }
                  } else {
                    apkInfoProvider.apkPath = result?.files.single.path;
                  }
                },
              )
            ],
          ),
          const Text(
            textAlign: TextAlign.start,
            "选择签名",
            style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
                color: Colors.deepPurple),
          ),
          SizedBox.fromSize(
            size: const Size(double.infinity, 300),
            child: GridView.builder(
              padding: const EdgeInsets.all(5),
              itemCount: mList.length,
              itemBuilder: (BuildContext context, int index) {
                return SignTypeItem(mList[index]);
              },
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 2,
                  mainAxisExtent: 100,
                  crossAxisSpacing: 5,
                  mainAxisSpacing: 5),
              // separatorBuilder: (BuildContext context, int index) {
              //   return const Divider(color: Colors.black, height: 1);
              // },
            ),
          )
        ]));
  }
}
